import java.util.Scanner;


public class UltimoDigitoFactorial {


	public static void main(String[] args) {
		Scanner leer = new Scanner(System.in);
		int cant = leer.nextInt();
			for (int i=0;i<cant;i++){
				int a = leer.nextInt();
				switch (a) {
				case 0:
				case 1:
					System.out.println(1);
					break;
				case 2:
					System.out.println(2);
					break;
				case 3:
					System.out.println(6);
					break;
				case 4:
					System.out.println(4);
					break;
				default:
					System.out.println(0);
					break;
				}
			}

	}

}
